#include <string>

using namespace std;

class Solution
{
public:
    int minAddToMakeValid(string s)
    {
        int ans = 0;
        int left = 0;
        for (auto x : s)
        {
            if (x == '(')
            {
                left++;
            }
            else
            {
                if (left)
                {
                    left--;
                }
                else
                {
                    ++ans;
                }
            }
        }
        return ans + left;
    }
};